package net.lzzy.lxq.server;

import net.lzzy.lxq.dao.AttendanceMapper;
import net.lzzy.lxq.entities.Attendance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceService {
    @Autowired
    AttendanceMapper mapper;

    public List<Attendance> getUserAttendance(String userId){
        return mapper.getUserAttendance(userId);
    }

    public void clockIn(Attendance attendance){
        mapper.add(attendance);
    }

    public List<Attendance> queryAttendanceFromToday(String userId){
        List<Attendance> userAttendanceToday=mapper.getUserAttendance(userId);
        List<Attendance> attendances=new ArrayList<>();
        Calendar todayCalender=Calendar.getInstance();
        int day=todayCalender.get(Calendar.DAY_OF_MONTH);
        int month=todayCalender.get(Calendar.MONTH)+1;
        int year=todayCalender.get(Calendar.YEAR);
        for(Attendance attendance:userAttendanceToday){
            Calendar attendanceCalender=Calendar.getInstance();
            attendanceCalender.setTime(new Date(attendance.getSwipeTime()));
            int day2=attendanceCalender.get(Calendar.DAY_OF_MONTH);
            int month2=attendanceCalender.get(Calendar.MONTH)+1;
            int year2=attendanceCalender.get(Calendar.YEAR);
            if (day==day2&&month==month2&&year2==year){
               attendances.add(attendance);
            }
        }
        return attendances;
    }

    public List<Attendance> getAll(){
        return mapper.getAll();
    }

    public boolean checkSwipeType(String userId,String type){
        List<Attendance> attendances=queryAttendanceFromToday(userId);
        for(Attendance ance:attendances){
            if (ance.getSwipeType().equals(type)){
                return true;
            }
        }
        return false;
    }

    public void add(Attendance attendance){
        mapper.add(attendance);
    }

}
